package com.mall.action;

import com.github.pagehelper.PageInfo;
import com.mall.bean.Goods;
import com.mall.service.GoodsService;
import com.mall.uitl.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/GoodsController")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    //   bookName:"",
    //    bookType:"",
    //    currentPage:1
    @RequestMapping(value = "/queryGoods", params = {"goods_name","cat_id","currentPage"})
    public JsonUtil queryGoods(Goods goodsBean, Integer currentPage){

        PageInfo<Goods> pageInfo =  goodsService.queryDataByPage(currentPage,5,goodsBean);

        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(pageInfo);

        return jsonUtil;
    }

    @RequestMapping(value = "/updateGoods")
    public JsonUtil updateGoods(@Validated Goods goods, BindingResult bindingResult){
        List<FieldError> fieldErrorList =  bindingResult.getFieldErrors();

        System.out.println("goods = " + goods);

        JsonUtil jsonUtil = new JsonUtil();


        if (fieldErrorList.size()>0){
            StringBuilder stringBuilder = new StringBuilder();
            for(FieldError error:fieldErrorList){
                /// System.out.println("字段:"+error.getField() +"校验未通过:"+error.getDefaultMessage());
                stringBuilder.append("字段").append(error.getField()).append("校验未通过").append(error.getDefaultMessage()).append("   ");

            }
            jsonUtil.setMsg(stringBuilder.toString());
            jsonUtil.setCode(1);
        }else{
            goodsService.update(goods);
        }


        return  jsonUtil;

    }

    @RequestMapping(value = "/insertGoods")
    public JsonUtil insertGoods(@Validated Goods goods, BindingResult bindingResult){
        List<FieldError> fieldErrorList =  bindingResult.getFieldErrors();

        System.out.println("goods = " + goods);

        JsonUtil jsonUtil = new JsonUtil();


        if (fieldErrorList.size()>0){
            StringBuilder stringBuilder = new StringBuilder();
            for(FieldError error:fieldErrorList){
                /// System.out.println("字段:"+error.getField() +"校验未通过:"+error.getDefaultMessage());
                stringBuilder.append("字段").append(error.getField()).append("校验未通过").append(error.getDefaultMessage()).append("   ");

            }
            jsonUtil.setMsg(stringBuilder.toString());
            jsonUtil.setCode(1);
        }else{
            goodsService.insert(goods);
        }


        return  jsonUtil;

    }

    @RequestMapping(value = "/deleteGoods" ,params = {"goods_id"})
    public JsonUtil deleteGoods(Goods goods){
        int i = goodsService.logicdelete(goods);
        JsonUtil jsonUtil = new JsonUtil();
        if(i == 1) {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("删除成功");
        }else{
            jsonUtil.setMsg("系统错误,删除失败");
            jsonUtil.setCode(1);
        }

        return  jsonUtil;

    }


//    @RequestMapping("/updateGoodsPic")
//    public  JsonUtil updateBookPic(@RequestParam("picture") MultipartFile pic, HttpServletRequest request) throws IOException, IOException {
//
//        String realPath =   request.getRealPath("img");
//
//        File parentFile = new File(realPath);
//        if (!parentFile.exists()){
//            parentFile.mkdirs();
//        }
//
//        File pictureFile = new File(parentFile,pic.getOriginalFilename());
//
//        pic.transferTo(pictureFile);
//
//        Integer port =  request.getLocalPort();//得到端口号
//        String ip  =   request.getLocalAddr();;//得到ip
//        String contextPath1 =   request.getContextPath();//得到上下文路径
//
//
//        String imgPath = "http://"+ip+":"+port+contextPath1+"/img/"+pic.getOriginalFilename();
//
//        JsonUtil jsonUtil = new JsonUtil();
//        jsonUtil.setData(imgPath);
//        jsonUtil.setCode(0);
//
//        return  jsonUtil;
//    }
}
